package com.kanqiubao.kq.http;

import com.google.gson.reflect.TypeToken;
import com.kanqiubao.kq.utils.common.LogUtils;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okgo.request.BaseRequest;

import okhttp3.Response;

public abstract class RequestCallback<T> extends AbsCallback<T> {
    private TypeToken<T> mTypeToken;

    public RequestCallback(TypeToken<T> token) {
        mTypeToken = token;
    }

    @Override
    public void onBefore(@SuppressWarnings("rawtypes") BaseRequest request) {
        HeadersUtils.getInstance().requestHeader(request);
        super.onBefore(request);
    }

    @Override
    public T convertSuccess(Response response) throws Exception {
        String t = StringConvert.create().convertSuccess(response);
        LogUtils.i("success---"+t);
        response.setBodyString(t);
        T data = Convert.fromJson(t, mTypeToken.getType());
        return data;
    }
}